import {
  dataSearch,
  dataUpdate,
  getOptMap,
  dataAdd,
  dataFindDataCol,
  matchAuthor,
} from '@/services/LabManagement';
import { message, notification } from 'antd';
import type { Reducer, Effect, Subscription } from 'umi';
import { connect, Link, history } from 'umi';
import { isLoginStatus, isLoginExpired } from '@/utils/loginStatusChecker';

export type StateType = {
  DataList?: any;
  DataListTemp?: any;
  OptMap?: any;
  CJXMap?: any;
};

export type StrategyType = {
  namespace: string;
  state: StateType;
  effects: {
    dataSearch: Effect;
    changeDate: Effect;
    dataUpdate: Effect;
    getOptMap: Effect;
    dataAdd: Effect;
    dataFindDataCol: Effect;
    matchAuthor: Effect;
  };
  reducers: {
    setDataList: Reducer<StateType>;
    setDataListTemp: Reducer<StateType>;
    setOptMap: Reducer<StateType>;
    setCJXMap: Reducer<StateType>;
  };
  subscriptions: {};
};

const Model: StrategyType = {
  namespace: 'labmanagement',
  state: {
    DataList: [], //
    DataListTemp: [], //
    OptMap: undefined,
    CJXMap: undefined,
  },
  effects: {
    *dataSearch({ payload }, { call, put }) {
      if (!isLoginStatus()) return;
      const token = localStorage.getItem('token');
      const response = yield call(dataSearch, token, payload);
      if (isLoginExpired(response)) return;
      if (response.data.code === 200) {
        yield put({
          type: 'setDataList',
          payload: response.data.data,
        });
      } else {
        message.error('获取信息失败！！！');
      }
    },
    *dataUpdate({ payload }, { call, put }) {
      if (!isLoginStatus()) return;
      const token = localStorage.getItem('token');
      const response = yield call(dataUpdate, token, payload);
      if (isLoginExpired(response)) return;
      if (response.data.code === 200) {
        console.log(response);

        // yield put({
        //     type: 'setDataList',
        //     payload: response.data.data,
        // });
      } else {
        message.error('获取信息失败！！！');
      }
    },
    *getOptMap({ payload }, { call, put }) {
      if (!isLoginStatus()) return;
      const token = localStorage.getItem('token');
      const response = yield call(getOptMap, token, payload);
      if (isLoginExpired(response)) return;
      if (response.data.code === 200) {
        yield put({
          type: 'setOptMap',
          payload: JSON.parse(response.data.data),
        });
      } else {
        message.error('获取信息失败！！！');
      }
    },
    *matchAuthor({ payload }, { call, put }) {
      if (!isLoginStatus()) return;
      const token = localStorage.getItem('token');
      console.log(payload);
      const response = yield call(matchAuthor, token, payload.payload);
      console.log(response);
      if (isLoginExpired(response)) return;
      if (response.data.code === 200 && response.data.data === true) {
        console.log(response);
        console.log(payload.value);
        if (payload.type === 'add') {
          const response1 = yield call(dataAdd, token, payload.value);
          console.log(response1);
        } else if (payload.type === 'edit') {
          const response2 = yield call(dataUpdate, token, payload.value);
          console.log(response2);
        }
        // yield put({
        //   type: 'setCJXMap',
        //   payload: response.data.data,
        // });
      } else {
        const args = {
          message: '添加失败！作者格式填写错误（英文分号）',
          description:
            '作者格式：张三;李四;王五  或者 张三 （英文名字中间用空格分隔）',
          duration: 0,
        };
        notification.error(args);
        // message.error("作者格式填写错误，");
      }
    },

    *dataAdd({ payload }, { call, put }) {
      if (!isLoginStatus()) return;
      const token = localStorage.getItem('token');
      const response = yield call(dataAdd, token, payload);
      if (isLoginExpired(response)) return;
      if (response.data.code === 200) {
        console.log(response);
        // yield put({
        //     type: 'setDataList',
        //     payload: response.data.data,
        // });
      } else {
        message.error(response.data.msg);
      }
    },
    *dataFindDataCol({ payload }, { call, put }) {
      if (!isLoginStatus()) return;
      const token = localStorage.getItem('token');
      const response = yield call(dataFindDataCol, token, payload);
      if (isLoginExpired(response)) return;
      if (response.data.code === 200) {
        console.log(response);
        yield put({
          type: 'setCJXMap',
          payload: response.data.data,
        });
      } else {
        message.error(response.data.msg);
      }
    },
    *changeDate({ payload }, { call, put }) {
      yield put({
        type: 'setDataList',
        payload: payload,
      });
    },
  },

  reducers: {
    setDataList(state, { payload }) {
      // console.log(payload.dataSource.map(item=>Object.keys(item)));
      return { ...state, DataList: payload };
    },
    setDataListTemp(state, { payload }) {
      return { ...state, DataListTemp: payload };
    },
    setOptMap(state, { payload }) {
      return { ...state, OptMap: payload };
    },
    setCJXMap(state, { payload }) {
      return { ...state, CJXMap: payload };
    },
  },

  subscriptions: {},
};

export default Model;
